package com.purong.purong.activity;

import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.purong.purong.R;
import com.purong.purong.fragment.ArticleListFragment;
import com.purong.purong.fragment.CreditCardFragment;
import com.purong.purong.fragment.HomeFragment;
import com.purong.purong.fragment.ArticleFragment;
import com.purong.purong.fragment.LoanFragment;
import com.purong.purong.fragment.MineFragment;
import com.purong.purong.fragment.TestFragment2;
import com.purong.purong.util.StatusBarUtil;


public class MainActivity extends BaseActivity implements TabHost.OnTabChangeListener {
    private FragmentTabHost mTabHost;
    private String mTabTitle[] = {"home", "borrow", "creditcard", "info", "mine"};
    private Fragment[] mFragments = new Fragment[5];
    private View[] mBottomViews = new View[5];
    private int[] mTabImgSel = {R.drawable.home_icon_sel, R.drawable.money_icon_sel, R.drawable.card_icon_sel, R.drawable.chat_icon_sel, R.drawable.profile_icon_sel};
    private int[] mTabImgNor = {R.drawable.home_icon_nor, R.drawable.money_icon_nor, R.drawable.card_icon_nor, R.drawable.chat_icon_nor, R.drawable.profile_icon_nor};
    public static boolean isCreate=false;

    private void initBottomView() {
        mBottomViews[0] = getLayoutInflater().inflate(R.layout.main_tab_home, null);
        mBottomViews[1] = getLayoutInflater().inflate(R.layout.main_tab_borrow, null);
        mBottomViews[2] = getLayoutInflater().inflate(R.layout.main_tab_creditcard, null);
        mBottomViews[3] = getLayoutInflater().inflate(R.layout.main_tab_info, null);
        mBottomViews[4] = getLayoutInflater().inflate(R.layout.main_tab_mine, null);
    }

    @Override
    public void initView() {
        isCreate=true;
        StatusBarUtil.setStatusBarTran(this);
        initBottomView();
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        // 关联TabHost
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        //注意，监听要设置在添加Tab之前
        mTabHost.setOnTabChangedListener(this);

        mFragments[0] = new HomeFragment();
        mFragments[1] = new LoanFragment();
        mFragments[2] = new CreditCardFragment();
        mFragments[3] = new ArticleFragment();
        mFragments[4] = new MineFragment();
        //添加Tab
        for (int i = 0; i < 5; i++) {
            //生成TabSpec
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTabTitle[i]).setIndicator(mBottomViews[i]);
            // 添加Tab 到TabHost，并绑定Fragment
            Bundle bundle = new Bundle();
            bundle.putString("from", "FragmentTabHost Tab");
            mTabHost.addTab(tabSpec, mFragments[i].getClass(), bundle);
        }


        //去掉Tab 之间的分割线
        mTabHost.getTabWidget().setDividerDrawable(null);
        //
        mTabHost.setCurrentTab(0);
        mTabHost.getTabWidget().getChildTabViewAt(1)
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mTabHost.getCurrentTab() == 1) {
                            try {
                                if (mTabTitle != null && mTabTitle.length >= 2) {
                                    ((LoanFragment) getSupportFragmentManager().findFragmentByTag(mTabTitle[1])).scroll2Top();
                                }
                            } catch (Exception e) {

                            }

                        } else {
                            mTabHost.setCurrentTab(1);
                        }

                    }
                });
        mTabHost.getTabWidget().getChildTabViewAt(3)
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mTabHost.getCurrentTab() == 3) {
                            try {
                                if (mTabTitle != null && mTabTitle.length >= 4) {
                                    ((ArticleFragment) getSupportFragmentManager().findFragmentByTag(mTabTitle[3])).scroll2Top();
                                }
                            } catch (Exception e) {

                            }

                        } else {
                            mTabHost.setCurrentTab(3);
                        }

                    }
                });
    }

    @Override
    public void initData() {

    }

    @Override
    public int getRootViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void onTabChanged(String tabId) {
        updateTabState();
    }

    /**
     * 更新Tab 的状态
     */
    private void updateTabState() {
        TabWidget tabWidget = mTabHost.getTabWidget();
        for (int i = 0; i < tabWidget.getTabCount(); i++) {
            View view = tabWidget.getChildTabViewAt(i);
            ImageView tabIcon = (ImageView) view.findViewById(R.id.image);
            TextView tabText = (TextView) view.findViewById(R.id.text);
            if (i == mTabHost.getCurrentTab()) {
                tabIcon.setImageResource(mTabImgSel[i]);
                tabText.setTextColor(getResources().getColor(R.color.blue));
            } else {
                tabIcon.setImageResource(mTabImgNor[i]);
                tabText.setTextColor(getResources().getColor(R.color.text_main_tab));
            }
        }
    }


    public void jumpLoan() {
        try {
            mTabHost.setCurrentTab(1);
        } catch (Exception e) {
        }
    }
    public void jumpArticle() {
        try {
            mTabHost.setCurrentTab(3);
        } catch (Exception e) {
        }
    }
    public void jumpArticleCredit() {
            try {
                ArticleFragment.jumpToCredit=true;
                mTabHost.setCurrentTab(3);
            } catch (Exception e) {
                Log.e("zwj",e.toString());
            }

    }
    //返回键退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("zwj", "......onKeyDown_main");
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);

    }
    private long exitTime = 0;

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            showToast("再按一次返回键退出", Toast.LENGTH_SHORT);
            exitTime = System.currentTimeMillis();
        } else {
            this.finish();
            System.exit(0);
        }
    }
}
